// pages/shop/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    edit: false,
    edit_name: "编辑",
    lists: [
      { "check": false, "img": "/images/temp_5.jpg", "title": "预售1！！2019高考试题猜想 最后一卷学科网学而思联合出品", "type": "文学", "price": "210", "quantity": 1, "edit": false, "total": 210 },
      { "check": false, "img": "/images/temp_5.jpg", "title": "预售2！！2019高考试题猜想 最后一卷学科网学而思联合出品", "type": "文学", "price": "220", "quantity": 1, "edit": false, "total": 220 },
      { "check": false, "img": "/images/temp_5.jpg", "title": "预售3！！2019高考试题猜想 最后一卷学科网学而思联合出品", "type": "文学", "price": "230", "quantity": 1, "edit": false, "total": 230 },
      { "check": false, "img": "/images/temp_5.jpg", "title": "预售4！！2019高考试题猜想 最后一卷学科网学而思联合出品", "type": "文学", "price": "240", "quantity": 1, "edit": false, "total": 240 }
    ],
    select_all: false,
    total: 0
  },
  // 编辑购物车
  edit_goods: function (e) {
    var edit = this.data.edit?false:true
    var edit_name = edit ? "完成" : "编辑";
    var lists = this.data.lists
    var total = 0
    for (var i in lists) {
      lists[i].edit = edit
      total += lists[i].check ? lists[i].total : 0
    }
    this.setData({ lists: lists, edit_name: edit_name, edit: edit, total: total })
  },
  // 购物车选中
  goods_select: function (e) {
    var key = e.currentTarget.dataset.key
    var lists = this.data.lists
    var check_total = 0;
    var total = 0
    for (var i in lists) {
      if (i == key){
        lists[i].check = lists[i].check ? false : true
      }
      check_total = lists[i].check ? check_total + 1 : check_total
      total += lists[i].check ? lists[i].total : 0
    }
    var select_all = lists.length == check_total ? true : false
    console.log(check_total)
    this.setData({ lists: lists, select_all: select_all, total: total })
  },
  // 购物车全选反选
  select_all: function() {
    var select_all = this.data.select_all ? false : true
    var check = select_all ? true : false
    var lists = this.data.lists
    var total = 0
    for (var i in lists) {
      lists[i].check = check
      total += check ? lists[i].total : 0
    }
    this.setData({ lists: lists, select_all: select_all, total: total }) 
  },
  // 删除购物车商品
  delete_cart: function(e){
    var lists = this.data.lists
    var key = e.currentTarget.dataset.key
    lists.splice(key,1)
    this.setData({ lists: lists })
  },
  // 设置购物车商品数量
  set_quantity: function(e){
    var _this = this
    var key = e.currentTarget.dataset.key
    var quantity = e.currentTarget.dataset.quantity
    var lists = _this.data.lists
    for (var i in lists) {
      if(i==key){
        var temp_quantity = lists[i].quantity + parseInt(quantity)
        if (temp_quantity<=0){
          wx.showModal({
            title: '提示',
            content: '确定要删除吗？',
            success: function (sm) {
              if (sm.confirm) {
                lists.splice(key, 1)
                for (var i in lists) {
                }
                _this.setData({ lists: lists, total: total })
              }
            }
          })
        }else{
          lists[i].quantity = temp_quantity
          lists[i].total = temp_quantity * lists[i].price
        }
        break;
      }
    }
    _this.setData({ lists: lists })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})